$(function () {
    initialPage();
    getGrid();
});


function initialPage() {
    $(window).resize(function () {
        TreeGrid.table.resetHeight({height: $(window).height() - 100});
    });
}

function getGrid() {
    var colunms = TreeGrid.initColumn();
    var table = new TreeTable(TreeGrid.id, '../storage/classify/queryAll', colunms);
    table.setExpandColumn(2);
    table.setIdField("id");
    table.setCodeField("id");
    table.setParentCodeField("parentId");
    table.setExpandAll(true);
    table.setHeight($(window).height() - 100);
    table.init();
    TreeGrid.table = table;
}

var TreeGrid = {
    id: "jqGrid",
    table: null,
    layerIndex: -1
};


/**
 * 初始化表格的列
 */
TreeGrid.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '分类名称', field: 'name', align: 'center', valign: 'middle', width: 180},
        {title: '上级分类', field: 'parentName', align: 'center', valign: 'middle', width: 100},
        {
            title: '操作', field: 'operator', width: 80,
            formatter: function (item) {
                return '<div><i-button type="info" style="cursor: pointer;" onclick="vm.add(\'' + item.id + '\')"><i class="fa fa-plus"></i>&nbsp;</i-button>' +
                    '<i-button type="info" style="cursor: pointer;" onclick="vm.update(\'' + item.id + '\')"><i class="fa fa-edit"></i>&nbsp;</i-button>' +
                    '<i-button type="info" style="cursor: pointer;" onclick="vm.del(\'' + item.id + '\')"><i class="fa fa-trash-o"></i>&nbsp;</i-button></div>';

            }
        }
      ]
    return columns;
};
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url: "nourl"
        }
    }
};

var ztree;

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        reset: true,
        storageClassify: {
            parentName: null,
            parentId: '01'
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'},
                {type:'string',message:'名称长度不能超过20',max:20}
            ]
        }
    },
    methods: {
    	getWinClassifyTree: function () {
            Ajax.request({
                url: "../storage/classify/queryAll",
                async: true,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#storageClassifyTree"), setting, r.list);
                    var node = ztree.getNodeByParam("id", vm.storageClassify.parentId);
                    if (node) {
                        ztree.selectNode(node);
                        vm.storageClassify.parentName = node.name;
                    } else {
                        vm.storageClassify.parentName = '一级分类';
                    }
                }
            });
        },

        add: function (event) {
            vm.showList = false;
            vm.reset = true;
            vm.title = "新增";
            var id = TreeGrid.table.getSelectedRow();
            var parentId = '';
            if (id.length != 0) {
                parentId = id[0].id;
            }
            if(typeof(event)!="object"){
                parentId =event;
            }
            vm.storageClassify = {parentName: null, parentId: parentId,status:0};
            vm.getWinClassifyTree();
        },
        update: function (event) {
            var id = null;
            if(typeof(event)!="object"){
                id =event;
            }else{
                id = getstorageClassifyId();
                if (id == false) {
                    return;
                }
            }
            Ajax.request({
                url: "../storage/classify/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.showList = false;
                    vm.reset = false;
                    vm.title = "修改";
                    vm.storageClassify = r.storageClassify;
                    vm.getWinClassifyTree();
                }
            });
        },

        saveOrUpdate: function (event) {
            var url = vm.storageClassify.id == null ? "../storage/classify/save" : "../storage/classify/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.storageClassify),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = new Array();
            if(typeof(event)!="object"){
            	ids.add(event);
            }else{
                var id = getStorageClassifyId();
                ids.add(id);
                if (id == false){
                    return;
                }
            }
            vm.storageClassify.id = ids[0];
            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../storage/classify/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (seriesId) {
            Ajax.request({
                url: "../storage/classify/info/" + seriesId,
                async: true,
                successCallback: function (r) {
                    vm.storageClassify = r.storageClassify;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            TreeGrid.table.refresh();
        },

        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});

function getStorageClassifyId() {
    var selected = $('#jqGrid').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        iview.Message.error("请选择一条记录");
        return false;
    } else {
        return selected[0].id;
    }
}

